from flask import Flask,request,Blueprint,json

# 注册蓝图
user_bp = Blueprint('user',__name__)


@user_bp.route('/add/',methods=['POST','GET'])
def add():
    if request.method == 'POST':
        data =json.loads(request.data)
        title = data['title']
        print(title)
        return 'ok' 
    else:
        return '展示'
        



import time

# 每次请求前钩子
@app.before_request
def before_request():
    # 获取当前url
    url = request.url
    # 获取当前时间戳
    nowtime = time.time()
    sql="insert into visit(url,stime) values('%s',%d)"%(url,nowtime)
    print(url,nowtime)
    return (url,nowtime)


# 每次请求后更新结束时间
@app.after_request
def after_request(response):
    # 获取当前url
    url = request.url
    # 获取当前时间戳
    nowtime = time.time()
    #根据url去数据库中查询
    sql = "select * from visit where url='%s' and etime=''"%(url)
    res = db.findOne(sql)
    sql ="update visit set etime='%s' where id=%d"%(nowtime,res['id'])
    #结束更新时间
    return make_response(response)